package hhm.android.my

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.alibaba.android.arouter.launcher.ARouter
import hhm.android.base.adapter.HBaseAdapter
import hhm.android.library.bus.Bus
import hhm.android.library.utils.GlideUtils
import siau.android.base.HttpHelper
import siau.android.base.SBApplication
import siau.android.base.WEBVIEW_ROUTER
import siau.android.base.getSureDialog
import siau.android.http.model.NotifyIdModel
import siau.android.http.model.MessageListModel
import siau.android.library.KeyString
import java.text.SimpleDateFormat
import java.util.*

class MessageListAdapter(var context: Context, list: ArrayList<MessageListModel>) : HBaseAdapter<MessageListViewHolder, MessageListModel>(list) {
    override fun bindView(parent: ViewGroup, viewType: Int): MessageListViewHolder {
        val v = LayoutInflater.from(context).inflate(R.layout.item_message_list, parent, false)
        val vh = MessageListViewHolder(v)

        vh.num.background = context.getDrawable(R.drawable.rectangle_circle_ff4e4e)
        return vh
    }

    override fun bindData(holder: MessageListViewHolder, bean: MessageListModel, position: Int) {
        if (position == list.size - 1) {
            holder.line.visibility = View.GONE
        } else {
            holder.line.visibility = View.VISIBLE
        }
        val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
        holder.name.text = bean.title
        holder.time.text = sdf.format(bean.pullTime)
        holder.content.text = bean.subContent
        if (bean.unreadCount == 0) {
            holder.num.visibility = View.GONE
        } else {
            holder.num.visibility = View.VISIBLE
            holder.num.text = bean.unreadCount.toString()
        }

        when (bean.notifyType) {
            1 -> {
                holder.rl.setOnClickListener {}
            }
            2 -> {
                GlideUtils.loadCircleImage(context, bean.avatar ?: "", holder.iv, R.mipmap.other_identity_icon, R.mipmap.other_identity_icon)
                holder.rl.setOnClickListener {}
            }
            3 -> {
                GlideUtils.loadCircleImage(context, bean.avatar ?: "", holder.iv, R.mipmap.ic_launcher, R.mipmap.ic_launcher)

                holder.rl.setOnClickListener {
                    val i = Intent(context, MessageSystemListActivity::class.java)
                    i.putExtra(KeyString.id, bean.notifyId)
                    context.startActivity(i)
                    bean.unreadCount = 0
                    notifyItemChanged(holder.layoutPosition)
                    SBApplication.userData.unreadCount = calculateUnReadNUm()
                }
            }
            4 -> {
                GlideUtils.loadCircleImage(context, bean.avatar ?: "", holder.iv, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
                holder.rl.setOnClickListener {
                    if (!TextUtils.isEmpty(bean.notifyLink)) {
                        //跳转h5
                        ARouter.getInstance().build(WEBVIEW_ROUTER).withString(KeyString.url, bean.notifyLink).navigation()

                        val m = NotifyIdModel(bean.notifyId)
                        HttpHelper().readMark(m)?.subscribe({
                            bean.unreadCount = 0
                            notifyItemChanged(holder.layoutPosition)
                            SBApplication.userData.unreadCount = calculateUnReadNUm()
                        }, {
                            Toast.makeText(context, it.message, Toast.LENGTH_SHORT).show()
                        })
                    }
                }
            }
        }
        holder.delete.setOnClickListener {
            getSureDialog(
                context, {
                    val m = NotifyIdModel(bean.notifyId)
                    HttpHelper().hideNotify(m)?.subscribe({
                        removeData(holder.layoutPosition)
                        SBApplication.userData.unreadCount = calculateUnReadNUm()
                    }, {
                        Toast.makeText(context, it.message, Toast.LENGTH_SHORT).show()
                    })
                }, showTitle = false, content = context.getString(R.string.sure_delete_this_message)
            )
        }
        holder.hasRead.setOnClickListener {
            val m = NotifyIdModel(bean.notifyId)
            HttpHelper().readMark(m)?.subscribe({
                bean.unreadCount = 0
                notifyItemChanged(holder.layoutPosition)
                SBApplication.userData.unreadCount = calculateUnReadNUm()
            }, {
                Toast.makeText(context, it.message, Toast.LENGTH_SHORT).show()
            })
            holder.sml.smoothClose()
        }

    }

    private fun calculateUnReadNUm(): Int {
        var count = 0

        list.forEach {
            count += it.unreadCount
        }

        return count
    }
}